package com.iyao_app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.iyao_app.R;
import com.iyao_app.im.adapter.AttentionInterestMyAdapter;
import com.iyao_app.entry.MyInterestBean;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.mvp.other.OtherHomeActivity;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xue on 2017/11/24.
 * 关注我的
 */

public class InterestMyFragment extends Fragment implements AttentionInterestMyAdapter.ONCLICK, OnItemClickListener {

    int page = 1, pageSize = 1000, type = 2;
    public List<MyInterestBean> modelList = new ArrayList<>();
    @BindView(R.id.interest_my_hint)
    TextView mInterestMyHint;
    @BindView(R.id.interest_my_no)
    TextView mInterestMyNo;
    @BindView(R.id.recyclerview_friend)
    RecyclerView mRecyclerviewFriend;
    Unbinder unbinder;
    @BindView(R.id.iv_abnormal)
    ImageView mIvAbnormal;
    private RecyclerView recyclerView;
    private AttentionInterestMyAdapter attentionFriendAdapter;
    private String friendId;
    private String id;
    private String nickname;
    private String favicon;
    private int sex;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_interestmy, null);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_friend);

        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (getArguments()!=null){
            id = getArguments().getString(Contants.OTHER_FIELD);
        }
        if (TextUtils.isEmpty(id)) {
            User user = DbUtil.getUser();
            id = user.id;
            nickname = user.nickname;
            favicon = user.avatarUrl;
            sex = user.sex;
        }
        getData();
    }

    private void getData() {

        NetUtils.getInstance().getFriends(id,nickname,favicon,sex, page, pageSize, type, new NetCallBack() {
            @Override
            public void onSuccess(String message, final int code, String response, final ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    if (model != null) {
                        modelList = (List<MyInterestBean>) model.getModelList();
                        if (modelList.size() != Contants.ZERO) {
                            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                            attentionFriendAdapter = new AttentionInterestMyAdapter(getContext(), recyclerView, modelList, InterestMyFragment.this, mListener);
                            recyclerView.setAdapter(attentionFriendAdapter);
                        } else {
                            mInterestMyHint.setVisibility(View.VISIBLE);
                            mInterestMyNo.setVisibility(View.VISIBLE);
                            mIvAbnormal.setVisibility(View.VISIBLE);
                            mRecyclerviewFriend.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void onFail(String message, int code) {

            }
        }, MyInterestBean.class);
    }

    @Override
    public void onclick(int position) {
        Intent intent = new Intent(getContext(), OtherHomeActivity.class);
        intent.putExtra(Contants.TYPE, String.valueOf(modelList.get(position).getUserId()));
        startActivity(intent);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ToastUtil.showToast(getActivity(),"个人主页");
    }

    /**
     * 实现类，响应按钮点击事件
     */
    private AttentionInterestMyAdapter.MyClickListener mListener = new AttentionInterestMyAdapter.MyClickListener() {

        private int sex;
        private String favicon;
        private String nickName;

        @Override
        public void myOnClick(final int position, View v) {
            friendId = String.valueOf(modelList.get(position).getUserId());
            nickName = modelList.get(position).getNickName();
            favicon = modelList.get(position).getFavicon();
            sex = modelList.get(position).getSex();
            if (modelList.get(position).getUserId() != modelList.get(position).getFriendId()) {
                NetUtils.getInstance().followFriend(friendId, nickName, favicon, sex, new NetCallBack() {
                    @Override
                    public void onSuccess(String message, final int code, String response, final ResultModel model) {
                        if (code == Contants.SUCCESS_CODE) {
                            modelList.remove(position);
                            Log.e("----", modelList.toString());
                            if (position == Contants.ZERO) {
                                mInterestMyHint.setVisibility(View.VISIBLE);
                                mInterestMyNo.setVisibility(View.VISIBLE);
                                mIvAbnormal.setVisibility(View.VISIBLE);
                                mRecyclerviewFriend.setVisibility(View.GONE);
                            }
                            attentionFriendAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFail(String message, int code) {

                    }
                }, null);

            } else {
                Toast.makeText(getContext(), "您不能关注您自己", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public void onHiddenChanged(boolean hidden) {
        // TODO Auto-generated method stub
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getData();
        }
    }
}